// pages/chef-canteen/chef-canteen.js
const app = getApp()
import util from "../../utils/util.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[{
     per:'',
      num:''
    }],
    columns: [],
    date:'',
    currentDate: new Date().getTime()+ 86400000,
    activeIndex:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.data){
      console.log(JSON.parse(options.data))
      const { canteensList, temporaryId } = JSON.parse(options.data)
      const { date, list } = canteensList
      this.setData({ date, list, temporaryId, revise:true})
    }
    const departmentType = wx.getStorageSync("departmentType")
    this.setData({
      departmentType
    })
    this.getdet()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getdet() {
    app.reqFetch.personneldict({
      pageSize:1000,
    }, (res) => {
      let list=[]
      res.list.map(item=>{
       list.push(item.name)
      })
      this.setData({ columns: list})
    })
  },

  handleShowPre(e){
    console.log(e.currentTarget.dataset.index)
    this.setData({ perShow: true, activeIndex: e.currentTarget.dataset.index})
  },
  onPerConfirm(e){
    const { list,activeIndex}=this.data
    list[activeIndex].per = e.detail.value
    this.setData({ list, perShow: false})
  },
  addCanteen(e){
    console.log(e.currentTarget.dataset.index)
    if (e.currentTarget.dataset.index>0){
      this.delCanteen(e)
    } else{
    let {list} =this.data
    if(list.length>=4){
      return
    }
    list.push({
      per: '',
      num: '',
    })
    this.setData({ list})
    }
  },
  delCanteen(e){
    const index  = e.currentTarget.dataset.index
    const { list } = this.data
    list.splice(index, 1)
    this.setData({ list })
  },
  handelShowDate( e) {
    this.setData({ dateShow: true })
  },
  onClose() {
    this.setData({ dateShow: false })
    this.setData({ perShow: false })
  },
  dateConfirm(e) {
    this.setData({ date: util.formatTime(new Date(e.detail)).substring(0, 16), dateShow:false })
  },
  formatDate(date) {
    date = new Date(date)
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    return year + '-' + month + '-' + day
  },
  numChnage(e){
    const { index}=e.currentTarget.dataset
    const {list} =this.data
    const { value} = e.detail
    list[index].num = value
    this.setData({ list })
  },
  perChnage(e) {
    const { index } = e.currentTarget.dataset
    const { list } = this.data
    const { value } = e.detail
    list[index].per = value
    this.setData({ list })
  },
  handelConfirm(){
    const { list, date,per} =this.data
    let flag =false
    list.map(item=>{
      if (!item.num || !item.per){
        flag=true 
      }
    })
    if (!date){
      wx.showToast({
        title: '请填写用餐时间',
        icon: 'none'
      })
      return
    }

    if (flag){
      wx.showToast({
        title: '请填写申请人与用餐人数',
        icon:'none'
      })
        return
      }

    app.globalData.canteensList = {  date,list}
    if (this.data.revise){
      const { temporaryId}=this.data
      app.reqFetch.temporaryorderupdeteOrder({
        boxId: JSON.stringify({ list: { date, list } }),
        mealTime: date,
      temporaryId,
      }, (res) => {
     wx.redirectTo({
       url: '/pages/purchase-list/purchase-list',
     })
      })
    }else{
      app.reqFetch.temporaryordersaveOrder({
        sign: 2,
        boxId: JSON.stringify({ list: { date, list } }),
        mealTime: date
      }, (res) => {
        wx.navigateTo({
          url: '/pages/supply-menu/supply-menu',
        })
      })
    }



  }
})